package plugins.sharedmind.view.gmomo;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import plugins.sharedmind.MapSharingController;

public class LoginWindow {
	private JDialog login_dialog;
    private JButton login_button;
    private JLabel user_label;
    private JLabel password_label;
    private JTextField user_field;
    private JPasswordField password_field;
	private MapSharingController mpc;
    
    public LoginWindow(JFrame parent, MapSharingController mpc) {
    	this.mpc = mpc; 
    	this.login_dialog = new JDialog(parent);
        initComponents();
    	login_button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				login();
			}
    	});
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                setVisible(true);
            }
        });
    }
    
    protected void login() {
    	try {
			mpc.gmomoLogin(user_field.getText(), new String(password_field.getPassword()));
			this.login_dialog.setVisible(false);
			mpc.showGmomoContactListWindow();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this.login_dialog, 
					"Cannot log in, make sure your user and password are correct!", 
					"Login Failed", JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
	}

	/** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        this.user_label = new JLabel();
        this.password_label = new JLabel();
        this.user_field = new JTextField();
        this.password_field = new JPasswordField();
        this.login_button = new JButton();

        this.login_dialog.setDefaultCloseOperation(
        		javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    	this.login_dialog.setTitle("Google Account Login");
    	this.user_label.setText("User ID:");
    	this.password_label.setText("Password:");
        this.user_field.setText("");
        this.password_field.setText("");
        this.login_button.setText("Login");
        this.login_button.setActionCommand("Login");

        javax.swing.GroupLayout layout = 
        	new javax.swing.GroupLayout(this.login_dialog.getContentPane());
        this.login_dialog.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(this.user_label)
                            .addComponent(this.password_label))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(this.password_field, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                            .addComponent(this.user_field, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(85, 85, 85)
                        .addComponent(this.login_button, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(this.user_label)
                    .addComponent(this.user_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(this.password_label)
                    .addComponent(this.password_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(this.login_button)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        this.login_dialog.pack();
        this.login_dialog.setResizable(false);
    }
    
	public String getUserId() {
		return this.user_field.getText();
	}
	
	public void setVisible(boolean visibility) {
		this.login_dialog.setVisible(visibility);
	}
}